﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestStreamInsight.StreamInsightMeterService;
using System.IO;
using System.Threading;
using System.Configuration;


namespace TestStreamInsight
{
    class Program
    {
        static DateTime lastMeterEventDateTime = DateTime.MinValue;  //null datetime

        static void Main(string[] args)
        {
            RawMeterEventType meterEvent=null;

            MeterEventTypeClient meterClient = new MeterEventTypeClient();

            Console.WriteLine("Test Client Ready, Hit Enter to Start....");
            Console.ReadLine();

            string r="Y";

            while (r.ToUpper() == "Y")
            {
                string fileLocation = ConfigurationManager.AppSettings["FileLocation"].ToString();
                System.IO.StreamReader testData = new System.IO.StreamReader(fileLocation);
                while (!testData.EndOfStream)
                {
                    string line = testData.ReadLine();

                    meterEvent = lineToMeterEvent(line);

                    callMeterService(meterClient, meterEvent);
                    Console.WriteLine("Submitted: " + line);
                }

                Console.WriteLine();
                Console.WriteLine("Run Test Again (Y/N)");
                r = Console.ReadLine();
                
            }
        }

        static void callMeterService(MeterEventTypeClient meterClient, RawMeterEventType meterEvent)
        {
            if (lastMeterEventDateTime != DateTime.MinValue)
            {
                TimeSpan elapsed = meterEvent.EventTimeStamp.Subtract(lastMeterEventDateTime);
                if ((elapsed.Seconds >= 0) || (elapsed.Milliseconds >=0))
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("          Wait " + elapsed.Seconds.ToString() + " seconds, " + elapsed.Milliseconds.ToString() + "milliseconds");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Thread.Sleep(elapsed.Seconds * 1000);
                }
                
                Console.WriteLine();
            }

            lastMeterEventDateTime = meterEvent.EventTimeStamp;

            //**********************************************************************
            //Not sure about this next line.
            //essentially, this will reset the eventTimeStamp to the current time
            //this will allow me to reuse the data and base everything from midnight
            //**********************************************************************
            meterEvent.EventTimeStamp = DateTime.Now;

            meterClient.SubmitMeterEvent(meterEvent);
        }

        static RawMeterEventType lineToMeterEvent(string line)
        {
            RawMeterEventType meterEvent = new RawMeterEventType();

            string[] stringArray = new string[7];
            char[] delimit = new char[] { ',' };
            int i = 0;

            foreach (string substr in line.Split(delimit))
            {
                stringArray[i] = substr;
                i++;
            }

            meterEvent.DataCurrentReading = double.Parse(stringArray[0]);  //double
            meterEvent.DataCurrentTemp = double.Parse(stringArray[1]);     //double
            meterEvent.EventTimeStamp = DateTime.Parse(stringArray[2]); //DateTime
            meterEvent.EventType = stringArray[3];                      //String
            meterEvent.MeterHeadEndID = stringArray[4];                 //string
            meterEvent.MeterNumber = stringArray[5];                    //string
            meterEvent.MeterVendorID = stringArray[6];                  //string
            
            return meterEvent;
        }
    }

}
